using Godot;
using System.Collections.Generic;

public partial class GameState : Node
{
    // 单例引用
    public static GameState Instance { get; private set; }


    public static bool DoorIsOpen = false;




    private Dictionary<string, bool> _flags = new Dictionary<string, bool>()
    {
        // 初始化一些默认状态
        { "HasKey", true },
        { "FirstMet", false },
        { "QuestCompleted", false }
    };

    public override void _Ready()
    {
        Instance = this;

    }

    // 设置状态
    public void SetFlag(string flagName, bool value)
    {
        if (_flags.ContainsKey(flagName))
            _flags[flagName] = value;
        else
            _flags.Add(flagName, value);

        GD.Print($"[GameState] Flag Set: {flagName} = {value}");
    }

    // 获取状态 (默认为 false)
    public bool GetFlag(string flagName)
    {
        if (_flags.ContainsKey(flagName))
            return _flags[flagName];
        return false;
    }


    public void LockPlayer()
    {
        // 锁定玩家控制逻辑
        //1. 获取当前场景中玩家节点
        var player = GetTree().GetCurrentScene().FindChild("Player", true, false) as PlayerController;

        if (player != null)
        {
            player.EnableMovement = false;
            GD.Print("[GameState] Player locked.");
        }
        else
        {
            GD.PrintErr("[GameState] Player node not found to lock.");
        }
    }


    public void UnLockPlayer()
    {
        var player = GetTree().GetCurrentScene().FindChild("Player", true, false) as PlayerController;

        if (player != null)
        {
            player.EnableMovement = true;
            GD.Print("[GameState] Player unlocked.");
        }
    }

    #region 门开关接口

    [Signal]// 定义信号：当门的状态改变时发出 (门ID, 是否开启)
    public delegate void DoorStateChangedEventHandler(string doorTag, bool isOpen);
    public static void OpenDoor(string doorTag)
    {
        if (Instance != null)
        {
            Instance.EmitSignal(SignalName.DoorStateChanged, doorTag, true);
            GD.Print($"发送开门信号: {doorTag}");
        }
    }

    public static void CloseDoor(string doorTag)
    {
        if (Instance != null)
        {
            Instance.EmitSignal(SignalName.DoorStateChanged, doorTag, false);
        }
    }
    #endregion

    // #region 临时游戏状态存储 (用于对话


    // //level5


    // public int lv5_DeadZoneToturialDone = 2;
    // public int lv5_chat3_done = 3;
    // public int lv5_chat4_done = 4;
    // public int lv5_chat5_done = 5;
    // public List<bool> Level5_DialoguesDone = [.. new bool[10]];

    // #endregion


    # region AudioManager 接口封装

    public void StopMusic()
    {
        AudioManager.Instance.StopMusic();
    }
    # endregion
}